//
//  UIImageView+Extension.swift
//  Feicui
//
//  Created by people on 2018/6/9.
//  Copyright © 2018年 zhan. All rights reserved.
//

import UIKit
import AVFoundation
extension UIImageView {
    public func cutImageView(withCornRadius radius:CGFloat)  {
        self.layer.cornerRadius=radius
        self.layer.masksToBounds=true
    }
    ///获取网络视频第一帧
    public func setVideoFirstView(withVideoUrl url:String)  {
        let asset = AVURLAsset.init(url: URL.init(string: url)!, options: nil)
        let gen = AVAssetImageGenerator.init(asset: asset)
        
        gen.appliesPreferredTrackTransform = true
        
        let time = CMTimeMakeWithSeconds(0.0, preferredTimescale: 1)
        var actualTime : CMTime = CMTimeMakeWithSeconds(0, preferredTimescale: 600)
        
        do {
            let image = try gen.copyCGImage(at: time, actualTime: &actualTime)

            self.image = UIImage.init(cgImage: image)
        } catch  {
            print("错误")
            
        }
    }
    
}

extension UIImageView {
    public func loadGif(name: String) {
        DispatchQueue.global().async {
            let image = UIImage.gif(name: name)
            DispatchQueue.main.async {
                self.image = image
            }
        }
    }
    public func stopGif()  {
        DispatchQueue.main.async {
            self.image = nil
        }
    }

    @available(iOS 9.0, *)
    public func loadGif(asset: String) {
        DispatchQueue.global().async {
            let image = UIImage.gif(asset: asset)
            DispatchQueue.main.async {
                self.image = image
            }
        }
    }

}
